#include <podofo/podofo.h>
#include <iostream>

using namespace PoDoFo;

int main(int argc, char* argv[]) {
    if (argc != 4) {
        std::cout << "Usage: " << argv[0] << " <input1.pdf> <input2.pdf> <output.pdf>" << std::endl;
        return 1;
    }
    
    std::string input1 = argv[1];
    std::string input2 = argv[2];
    std::string output = argv[3];
    
    try {
        // 初始化 PoDoFo（某些版本需要）
        // PdfMemDocument::Init();
        
        PdfMemDocument outputDoc;
        
        // 合并第一个PDF
        PdfMemDocument doc1;
        doc1.Load(input1.c_str());
        std::cout << "First PDF pages: " << doc1.GetPageCount() << std::endl;
        
        for (int i = 0; i < doc1.GetPageCount(); i++) {
            PdfPage* sourcePage = doc1.GetPage(i);
            outputDoc.GetPages().CreatePage(sourcePage);
        }
        
        // 合并第二个PDF
        PdfMemDocument doc2;
        doc2.Load(input2.c_str());
        std::cout << "Second PDF pages: " << doc2.GetPageCount() << std::endl;
        
        for (int i = 0; i < doc2.GetPageCount(); i++) {
            PdfPage* sourcePage = doc2.GetPage(i);
            outputDoc.GetPages().CreatePage(sourcePage);
        }
        
        // 保存
        outputDoc.Save(output.c_str());
        std::cout << "Merged PDF saved as: " << output << std::endl;
        std::cout << "Total pages: " << outputDoc.GetPageCount() << std::endl;
        
    } catch (PdfError& e) {
        std::cerr << "PoDoFo Error: " << e.what() << std::endl;
        return e.GetError();
    } catch (std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }
    
    return 0;
}